package com.aigou.admin.dao.impl;

import com.aigou.admin.dao.SalesDAO;
import com.aigou.admin.entity.Sales;
import com.aigou.admin.util.JDBCUtils;

import java.util.List;

public class SalesDAOImpl implements SalesDAO {
    @Override
    public List<Sales> salesLimit(int start, int rows) {
        return JDBCUtils.executeQuery(Sales.class,"select * from s_sales limit ?,?",
                start,rows);
    }

    @Override
    public Integer selectsalesCount() {
        return JDBCUtils.executeCount("select count(*) from s_sales ");
    }

    @Override
    public List<Sales> setAll() {
        List<Sales> sales = JDBCUtils.executeQuery(Sales.class,"select * from s_sales");
        return sales;
    }

    @Override
    public Sales selectByid(String id) {
        List<Sales> sales = JDBCUtils.executeQuery(Sales.class,"select * from s_sales where id = ?",id);
        if (sales.isEmpty()){
            return null;
        }else {
            return sales.get(0);
        }
    }

    @Override
    public boolean inser(Sales sales) {
        int rows = JDBCUtils.executeUpdate("insert into s_sales(id,product_id,sales_num)values(?,?,?)",
                sales.getId(),sales.getProduct_id(),sales.getSales_num());
        return rows > 0;
    }

    @Override
    public boolean delete(String id) {
        int rows = JDBCUtils.executeUpdate("delete from s_sales where id = ?",id);
        return rows > 0;
    }

    @Override
    public boolean update(Sales sales) {
        int rows = JDBCUtils.executeUpdate("update s_sales set product_id = ? , sales_num = ? where id = ?",
                sales.getProduct_id(),sales.getSales_num(),sales.getId());
        return rows > 0;
    }
}
